// pages/city/city.js
const HomeHttp = require('../../../utils/HomeHttp');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        citys: [],
        //最近访问
        lately: [],
        value: "",
        arr: [],
        // 获取输入框焦点
        focus: false,
        // 搜索出来的结果
        searchCitys: []
    },
    clickCity(e) {
        var { city } = e.currentTarget.dataset;
        wx.setStorageSync("city", city);
        wx.navigateBack();
    },
    // 输入框聚焦时候触发
    onFocus() {
        var focus = true;
        this.setData({
            focus
        })
    },
    // 输入框失去焦点
    onBlur() {
        var focus = false;
        this.setData({
            focus
        })
    },
    // 搜索
    onSearch(e) {
        var value = e.detail;
        var searchCitys = [];
        this.data.arr.forEach(item => {
            item.list.forEach(c => {
                if (value) {
                    if (c.name.includes(value)) {
                        searchCitys.push(c.name)
                    }
                }
            })
        })
        this.setData({
            searchCitys
        })
    },
    // 点击搜索出来的结果
    clickSearchs(e) {
        var { city } = e.currentTarget.dataset;
        var lately = this.data.lately;
        lately.unshift(city);
        var value = ""
        this.setData({
            lately,
            value
        })
        wx.setStorageSync("city", city);
        wx.setStorageSync("lately", lately);
        wx.navigateBack();
    },
    // 删除最近访问
    handleDeleteBySearch() {
        var lately = [];
        wx.setStorageSync("lately", lately);
        this.setData({
            lately
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        wx.showLoading({
          title: '加载中...',
        })
        // 请求城市接口
        var res = await HomeHttp.getCity();

        var keys = Object.keys(res.data.res[0].data.cities);
        var arr = [];
        keys.forEach(item => {
            arr.push({
                firstLetter: item,
                list: res.data.res[0].data.cities[item]
            })
        });
        this.setData({
            citys: res.data.res[0].data,
            indexList: Object.keys(res.data.res[0].data.cities),
            arr,
        })
        wx.hideLoading({})
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // 页面显示拿值
        var lately = wx.getStorageSync('lately');
        if (lately) {
            this.setData({
                lately
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})